<!DOCTYPE html>
<html lang="zh-CN">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta name="robots" content="noodp" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge, chrome=1">
        <title>03-判断溢出和精度误差和名词解释 - Adbean&#39;s Blog</title><meta name="Description" content="Adbean&#39;s Blog"><meta property="og:title" content="03-判断溢出和精度误差和名词解释" />
<meta property="og:description" content="判断溢出错误和精度误差 Write out variables x, y and z in binary code int8_t x = 67; int8_t y = -7; int8_t z = y - x; int8_t x = 0xd3; uint8_t = 0xd3; int8_t x = 127; int8_t y = -7; int8_t z = y – x; float x = 1.125; float x = 23.0; float x = 0.45; Specifier Equivalent" />
<meta property="og:type" content="article" />
<meta property="og:url" content="https://adbean.gitee.io/posts/my-third-post/" />
<meta property="og:image" content="https://adbean.gitee.io/logo.png"/>
<meta property="article:published_time" content="2020-09-20T20:06:17+08:00" />
<meta property="article:modified_time" content="2020-09-20T20:06:17+08:00" />
<meta name="twitter:card" content="summary_large_image"/>
<meta name="twitter:image" content="https://adbean.gitee.io/logo.png"/>

<meta name="twitter:title" content="03-判断溢出和精度误差和名词解释"/>
<meta name="twitter:description" content="判断溢出错误和精度误差 Write out variables x, y and z in binary code int8_t x = 67; int8_t y = -7; int8_t z = y - x; int8_t x = 0xd3; uint8_t = 0xd3; int8_t x = 127; int8_t y = -7; int8_t z = y – x; float x = 1.125; float x = 23.0; float x = 0.45; Specifier Equivalent"/>
<meta name="application-name" content="Adbean&#39;s Blog">
<meta name="apple-mobile-web-app-title" content="Adbean&#39;s Blog"><meta name="theme-color" content="#ffffff"><meta name="msapplication-TileColor" content="#da532c"><link rel="icon" href="../../Owl.ico"><link rel="apple-touch-icon" sizes="180x180" href="../../apple-touch-icon.png"><link rel="mask-icon" href="../../safari-pinned-tab.svg" color="#5bbad5"><link rel="manifest" href="../../site.webmanifest"><link rel="canonical" href="https://adbean.gitee.io/posts/my-third-post/" /><link rel="prev" href="https://adbean.gitee.io/posts/my-second-post/" /><link rel="next" href="https://adbean.gitee.io/posts/my-first-project/" /><link rel="stylesheet" href="../../lib/normalize/normalize.min.css"><link rel="stylesheet" href="../../css/style.min.css"><link rel="stylesheet" href="../../lib/fontawesome-free/all.min.css"><link rel="stylesheet" href="../../lib/animate/animate.min.css"><script type="application/ld+json">
    {
        "@context": "http://schema.org",
        "@type": "BlogPosting",
        "headline": "03-判断溢出和精度误差和名词解释",
        "inLanguage": "zh-CN",
        "mainEntityOfPage": {
            "@type": "WebPage",
            "@id": "https:\/\/adbean.gitee.io\/posts\/my-third-post\/"
        },"genre": "posts","keywords": "名词解释, 作业, 精度","wordcount":  1617 ,
        "url": "https:\/\/adbean.gitee.io\/posts\/my-third-post\/","datePublished": "2020-09-20T20:06:17+08:00","dateModified": "2020-09-20T20:06:17+08:00","publisher": {
            "@type": "Organization",
            "name": "Adbean"},"author": {
                "@type": "Person",
                "name": "Adbean"
            },"description": ""
    }
    </script></head>
    <body header-desktop="fixed" header-mobile="auto"><script type="text/javascript">(window.localStorage && localStorage.getItem('theme') ? localStorage.getItem('theme') === 'dark' : ('dark' === 'auto' ? window.matchMedia('(prefers-color-scheme: dark)').matches : 'dark' === 'dark')) && document.body.setAttribute('theme', 'dark');</script>

        <div id="mask"></div><div class="wrapper"><header class="desktop" id="header-desktop">
    <div class="header-wrapper">
        <div class="header-title">
            <a href="../../" title="Adbean&#39;s Blog"><img
        class="lazyload logo"
        src="../../svg/loading.min.svg"
        data-src="../../Owl.ico"
        data-srcset="../../Owl.ico, ../../Owl.ico 1.5x, ../../Owl.ico 2x"
        data-sizes="auto"
        alt="/Owl.ico"
        title="/Owl.ico" />Adbean&#39;s Blog</a>
        </div>
        <div class="menu">
            <div class="menu-inner"><a class="menu-item" href="../../"> 主页 </a><a class="menu-item" href="../../posts/"> 文章 </a><a class="menu-item" href="../../posts/my-index/"> 索引 </a><a class="menu-item" href="../../tags/"> 标签 </a><a class="menu-item" href="../../categories/"> 分类 </a><span class="menu-item delimiter"></span><a href="javascript:void(0);" class="menu-item theme-switch" title="切换主题">
                    <i class="fas fa-adjust fa-fw"></i>
                </a>
            </div>
        </div>
    </div>
</header><header class="mobile" id="header-mobile">
    <div class="header-container">
        <div class="header-wrapper">
            <div class="header-title">
                <a href="../../" title="Adbean&#39;s Blog"><img
        class="lazyload logo"
        src="../../svg/loading.min.svg"
        data-src="../../Owl.ico"
        data-srcset="../../Owl.ico, ../../Owl.ico 1.5x, ../../Owl.ico 2x"
        data-sizes="auto"
        alt="/Owl.ico"
        title="/Owl.ico" />Adbean&#39;s Blog</a>
            </div>
            <div class="menu-toggle" id="menu-toggle-mobile">
                <span></span><span></span><span></span>
            </div>
        </div>
        <div class="menu" id="menu-mobile"><a class="menu-item" href="../../" title="">主页</a><a class="menu-item" href="../../posts/" title="">文章</a><a class="menu-item" href="../../posts/my-index/" title="">索引</a><a class="menu-item" href="../../tags/" title="">标签</a><a class="menu-item" href="../../categories/" title="">分类</a><a href="javascript:void(0);" class="menu-item theme-switch" title="切换主题">
                <i class="fas fa-adjust fa-fw"></i>
            </a></div>
    </div>
</header>
<div class="search-dropdown desktop">
    <div id="search-dropdown-desktop"></div>
</div>
<div class="search-dropdown mobile">
    <div id="search-dropdown-mobile"></div>
</div>
<main class="main">
                <div class="container"><div class="toc" id="toc-auto">
            <h2 class="toc-title">目录</h2>
            <div class="toc-content" id="toc-content-auto"></div>
        </div><article class="page single"><h1 class="single-title animated flipInX">03-判断溢出和精度误差和名词解释</h1><div class="post-meta">
            <div class="post-meta-line"><span class="post-author"><a href="https://adbean.gitee.io/" title="Author" target="_blank" rel="noopener noreffer author" class="author"><i class="fas fa-user-circle fa-fw"></i>Adbean</a></span>&nbsp;<span class="post-category">收录于 <a href="../../categories/%E4%BD%9C%E4%B8%9A/"><i class="far fa-folder fa-fw"></i>作业</a></span></div>
            <div class="post-meta-line"><i class="far fa-calendar-alt fa-fw"></i>&nbsp;<time datetime="2020-09-20">2020-09-20</time>&nbsp;<i class="fas fa-pencil-alt fa-fw"></i>&nbsp;约 1617 字&nbsp;
                <i class="far fa-clock fa-fw"></i>&nbsp;预计阅读 4 分钟&nbsp;</div>
        </div><div class="details toc" id="toc-static"  kept="">
                <div class="details-summary toc-title">
                    <span>目录</span>
                    <span><i class="details-icon fas fa-angle-right"></i></span>
                </div>
                <div class="details-content toc-content" id="toc-content-static"><nav id="TableOfContents">
  <ul>
    <li><a href="#判断溢出错误和精度误差">判断溢出错误和精度误差</a>
      <ul>
        <li><a href="#write-out-variables-x-y-and-z-in-binary-code">Write out variables x, y and z in binary code</a></li>
      </ul>
    </li>
    <li><a href="#explanation-of-terms-名词解释">Explanation of Terms 名词解释</a>
      <ul>
        <li><a href="#method-of-complements">Method of complements</a></li>
        <li><a href="#byte">Byte</a></li>
        <li><a href="#integer-computer-science">Integer (computer science)</a></li>
        <li><a href="#floating-point">Floating point</a></li>
      </ul>
    </li>
    <li><a href="#证明">证明</a>
      <ul>
        <li><a href="#1-请证明二进制的负数twos-complement-of-x等于-x-的-ones-complement--1即x-每位求反加-1">1. 请证明：二进制的负数（two&rsquo;s complement of X）等于 X 的 one’s complement ＋ 1（即，X 每位求反加 1）</a></li>
        <li><a href="#2-int8_t-x----017-请用-8-进制描述变量-x在-c-中-017-即-017_8-">2. Int8_t x = - 017; 请用 8 进制描述变量 x。在 c 中 017 即$ (017)_8 $</a></li>
        <li><a href="#3-c-程序int8_t-x---0x1f-int-y--x-请用-16-进制描述变量-x-和-y并说明-int-y--x-的计算过程">3. C 程序：int8_t x = -0x1f; int y = x; 请用 16 进制描述变量 x 和 y，并说明 int y = x 的计算过程。</a></li>
        <li><a href="#4-请用数学证明为什么可以这么计算">4. 请用数学证明，为什么可以这么计算。</a></li>
        <li><a href="#5-nan-是什么">5. NaN 是什么？</a></li>
      </ul>
    </li>
  </ul>
</nav></div>
            </div><div class="content" id="content"><h2 id="判断溢出错误和精度误差">判断溢出错误和精度误差</h2>
<h3 id="write-out-variables-x-y-and-z-in-binary-code">Write out variables x, y and z in binary code</h3>
<ol>
<li>int8_t x = 67; int8_t y = -7; int8_t z = y - x;</li>
<li>int8_t x = 0xd3;</li>
<li>uint8_t = 0xd3;</li>
<li>int8_t x = 127; int8_t y = -7; int8_t z = y – x;</li>
<li>float x = 1.125;</li>
<li>float x = 23.0;</li>
<li>float x = 0.45;</li>
</ol>
<table>
<thead>
<tr>
<th>Specifier</th>
<th>Equivalent</th>
<th>Signing</th>
<th>Bits</th>
<th>Bytes</th>
<th>Range</th>
</tr>
</thead>
<tbody>
<tr>
<td>int8_t</td>
<td>signed char</td>
<td>Signed</td>
<td>8</td>
<td>1</td>
<td>-128 ~ 127</td>
</tr>
<tr>
<td>uint8_t</td>
<td>unsigned char</td>
<td>Unsigned</td>
<td>8</td>
<td>1</td>
<td>0 ~ 255</td>
</tr>
<tr>
<td>float</td>
<td>single precision</td>
<td>Signed</td>
<td>32</td>
<td>4</td>
<td>-2^128 ~ +2^128</td>
</tr>
</tbody>
</table>
<p>由上表格可知：</p>
<ol>
<li>$$ x = (67)_ {10} = (0100 0011) _2, y = (-7)_{10} = (10111101)_2, z = (-74)_{10} = (1011 0110)_2 $$
        都在 int8_t 范围内所以没有产生溢出</li>
<li>$$ (0xd3)_ {16} = (211)_{10} = (1101 0011)_2 $$
        0xd3 转换成十进制为 $$ 3 * 16^0 + 13 * 16^1 = 211 &gt; 127 $$
        所以溢出 int8*t 的范围</li>
<li>$$ (0xd3)_{16} = (211) _{10} $$
        而 uint8_t 的范围是 0 ~ 255，所以没有溢出</li>
<li>$$ x = (127)_ {10} = (01111111) _2, y = (-7)_{10} = (10111101)_2, z = (-134)_{10} = (0111 1010)_2 $$
        其中 x 和 y 没有溢出，但是 z 产生溢出</li>
<li>$$ (1.125)_{10} = (1.001)_2 $$
        其二进制不是循环小数故没有产生精度误差</li>
<li>$$ (23.0)_{10} = (0001 0111)_2 $$
        其二进制不是循环小数故没有产生精度误差</li>
<li>$$ (0.45)_{10} = (0.01 1100 1100 1100&hellip;)_2 $$
        由于其二进制是循环小数所以会丢失部分精度</li>
</ol>
<h2 id="explanation-of-terms-名词解释">Explanation of Terms 名词解释</h2>
<h3 id="method-of-complements">Method of complements</h3>
<p>In mathematics and computing, <strong>the method of complements</strong> is a technique to encode a symmetric range of positive and negative integers in a way that they can use the same algorithm (hardware) for addition throughout the whole range. For a given number of places half of the possible representations of numbers encode the positive numbers, the other half represents their respective additive inverses. The pairs of mutually additive inverse numbers are called <strong>complements</strong>.</p>
<p>在数学和计算中，<strong>补数方法</strong>是一种对正负整数的对称范围进行编码的技术，使它们可以在整个范围内使用相同的算法（硬件）进行加法运算。对于给定的位置数，可能的数字表示形式中的一半编码为正数，另一半表示其相应的加法逆。 成对的相互加反数称为补数。</p>
<h3 id="byte">Byte</h3>
<p>The <strong>byte</strong> is a unit of digital information that most commonly consists of eight <strong>bits</strong>. Historically, the byte was the number of bits used to <strong>encode</strong> a single character of text in a computer and for this reason it is the smallest addressable unit of memory in many computer architectures.</p>
<p><strong>字节</strong>是数字信息的单位，通常由<strong>八位</strong>组成。 从历史上看，字节是计算机中用于<strong>编码</strong>文本的单个字符的位数，因此，它是许多计算机体系结构中最小的可寻址存储单元。</p>
<h3 id="integer-computer-science">Integer (computer science)</h3>
<p>An <strong>integer</strong> (from the Latin <em>integer</em> meaning &ldquo;whole&rdquo;) is colloquially defined as a number that can be written without a fractional component. For example, 21, 4, 0, and −2048 are <strong>integers</strong>, while 9.75, $ 5\frac{1}{2} $
, and $ \sqrt{2} $ are not.</p>
<p><strong>整数</strong>（源自拉丁文<em>整数</em>，表示“整个”）的口语定义为无需小数部分即可写入的数字。 例如，21、4、0 和-2048 是整数，而 9.75、$ 5\frac{1}{2} $ 、$\sqrt{2} $ 不是。</p>
<h3 id="floating-point">Floating point</h3>
<p>In computing, <strong>floating-point</strong> arithmetic (FP) is arithmetic using formulaic representation of real numbers as an approximation to support a trade-off between range and precision. For this reason, floating-point computation is often found in systems which include very small and very large real numbers, which require fast processing times.</p>
<p>在计算中，<strong>浮点</strong>算术（FP）是使用实数形式表示作为近似值以支持范围和精度之间的算术。 因此，在包含非常小和非常大的实数的系统中经常会发现浮点计算，这需要快速的处理时间。</p>
<h2 id="证明">证明</h2>
<h3 id="1-请证明二进制的负数twos-complement-of-x等于-x-的-ones-complement--1即x-每位求反加-1">1. 请证明：二进制的负数（two&rsquo;s complement of X）等于 X 的 one’s complement ＋ 1（即，X 每位求反加 1）</h3>
<p>$ Proof: $
首先因为负数就是做减法，可以用模的定义来解释减法，减去一个数等同于加上这个减数的同余数。比如 10 是模，十减二(10 - 3)，等同于 10 - 3 = 7 所以此时 7 可以表示 -3 ，而(+7)是通过|10 - 3| = 7 得来的。</p>
<p>因此有：负数（等价意义）= 模 - |负数|</p>
<p>下面证明为什么补码等于反码加一，可以举例说明： $ (-2)_{10} $的反码是 1101，而反码的绝对值是 0010 也就是正数 2，由整数加减法我们可知 -2 + 2 = 0，但由二进制加减法 $ 1101 + 0010 = 1111 \neq 0 $ 于是我们再加上一个 1 则可以表示为 0 了，此时模为 10000，即取低 4 位。</p>
<p>综上我们可以知道二进制的负数也即负数的补码，等于其二进制的反码加一。</p>
<h3 id="2-int8_t-x----017-请用-8-进制描述变量-x在-c-中-017-即-017_8-">2. Int8_t x = - 017; 请用 8 进制描述变量 x。在 c 中 017 即$ (017)_8 $</h3>
<p>解：$ (017)_8 = (15) _{10} = (0000 1111)_2 $ 所以 -15 的二进制为$ (1111 0001)_2 $ 转换成八进制为 361</p>
<h3 id="3-c-程序int8_t-x---0x1f-int-y--x-请用-16-进制描述变量-x-和-y并说明-int-y--x-的计算过程">3. C 程序：int8_t x = -0x1f; int y = x; 请用 16 进制描述变量 x 和 y，并说明 int y = x 的计算过程。</h3>
<p>解：因为$ (0x1f)_{16} = (31) _{10} = (00011111) _2 $ 所以计算机先把 8 位 int8_t 型变量 x 转为二进制并以补码形式储存(11100001)，并且通过扩展编码扩充为 32 位即 4 个字节 (11111111111111111111111111100001) 存在 int 型变量 y 中。</p>
<h3 id="4-请用数学证明为什么可以这么计算">4. 请用数学证明，为什么可以这么计算。</h3>
<p>$ Proof: $</p>
<p>$$
设A = (1X)_ 2 = (-2^{n-1} )_ {10} + (X)_ 2
$$</p>
<p>$$
B = (11&hellip;&hellip;11X)_ 2 = (-2^ {m-1}+2^{m-2}+&hellip;&hellip;+2^n+2^{n-1})_ {10}+(X)_ 2
$$</p>
<p>$$
=(2^{n - 1} \frac{(1 - 2^{m-2-(n-1)+1})}{1 - 2})=(-2^{n-1})_ {10} + (X)_2 = A
$$</p>
<p>综上，符号扩展正确的</p>
<h3 id="5-nan-是什么">5. NaN 是什么？</h3>
<p>IEEE 754 specifies a special value called &ldquo;Not a Number&rdquo; (NaN) to be returned as the result of certain &ldquo;invalid&rdquo; operations, such as 0/0, ∞×0, or sqrt(−1). In general, NaNs will be propagated i.e. most operations involving a NaN will result in a NaN.</p>
<p>IEEE 754 指定一个特殊值“ Not a Number”（NaN）作为某些“无效”操作（例如 0/0，∞×0 或 sqrt（-1））的结果返回。大多数涉及 NaN 的操作都将生成 NaN。</p>
</div><div class="post-footer" id="post-footer">
    <div class="post-info">
        <div class="post-info-line">
            <div class="post-info-mod">
                <span>更新于 2020-09-20</span>
            </div>
            <div class="post-info-license"></div>
        </div>
        <div class="post-info-line">
            <div class="post-info-md"></div>
            <div class="post-info-share">
                <span></span>
            </div>
        </div>
    </div>

    <div class="post-info-more">
        <section class="post-tags"><i class="fas fa-tags fa-fw"></i>&nbsp;<a href="../../tags/%E5%90%8D%E8%AF%8D%E8%A7%A3%E9%87%8A/">名词解释</a>,&nbsp;<a href="../../tags/%E4%BD%9C%E4%B8%9A/">作业</a>,&nbsp;<a href="../../tags/%E7%B2%BE%E5%BA%A6/">精度</a></section>
        <section>
            <span><a href="javascript:void(0);" onclick="window.history.back();">返回</a></span>&nbsp;|&nbsp;<span><a href="../../">主页</a></span>
        </section>
    </div>

    <div class="post-nav"><a href="../../posts/my-second-post/" class="prev" rel="prev" title="02-使用维基百科解释名词和写出英文单词"><i class="fas fa-angle-left fa-fw"></i>02-使用维基百科解释名词和写出英文单词</a>
            <a href="../../posts/my-first-project/" class="next" rel="next" title="使用 Hogo &#43; LoveIt 构建博客">使用 Hogo &#43; LoveIt 构建博客<i class="fas fa-angle-right fa-fw"></i></a></div>
</div>
</article></div>
            </main><footer class="footer">
        <div class="footer-container"><div class="footer-line">由 <a href="https://gohugo.io/" target="_blank" rel="noopener noreffer" title="Hugo 0.74.3">Hugo</a> 强力驱动 | 主题 - <a href="https://github.com/dillonzq/LoveIt" target="_blank" rel="noopener noreffer" title="LoveIt 0.2.10"><i class="far fa-kiss-wink-heart fa-fw"></i> LoveIt</a>
                </div><div class="footer-line"><i class="far fa-copyright fa-fw"></i><span itemprop="copyrightYear">2020 - 2021</span><span class="author" itemprop="copyrightHolder">&nbsp;<a href="https://adbean.gitee.io/" target="_blank">Adbean</a></span>&nbsp;|&nbsp;<span class="license"><a rel="license external nofollow noopener noreffer" href="https://creativecommons.org/licenses/by-nc/4.0/" target="_blank">CC BY-NC 4.0</a></span></div>
        </div>
    </footer></div>

        <div id="fixed-buttons"><a href="#" id="back-to-top" class="fixed-button" title="回到顶部">
                <i class="fas fa-arrow-up fa-fw"></i>
            </a><a href="#" id="view-comments" class="fixed-button" title="查看评论">
                <i class="fas fa-comment fa-fw"></i>
            </a>
        </div><link rel="stylesheet" href="../../lib/katex/katex.min.css"><link rel="stylesheet" href="../../lib/katex/copy-tex.min.css"><script type="text/javascript" src="../../lib/smooth-scroll/smooth-scroll.min.js"></script><script type="text/javascript" src="../../lib/lazysizes/lazysizes.min.js"></script><script type="text/javascript" src="../../lib/clipboard/clipboard.min.js"></script><script type="text/javascript" src="../../lib/katex/katex.min.js"></script><script type="text/javascript" src="../../lib/katex/auto-render.min.js"></script><script type="text/javascript" src="../../lib/katex/copy-tex.min.js"></script><script type="text/javascript" src="../../lib/katex/mhchem.min.js"></script><script type="text/javascript">window.config={"code":{"copyTitle":"复制到剪贴板","maxShownLines":50},"comment":{},"math":{"delimiters":[{"display":true,"left":"$$","right":"$$"},{"display":true,"left":"\\[","right":"\\]"},{"display":false,"left":"$","right":"$"},{"display":false,"left":"\\(","right":"\\)"}],"strict":false}};</script><script type="text/javascript" src="../../js/theme.min.js"></script></body>
</html>
